package cn.internship.modules.manage.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.AbstractExecutorService;

import cn.internship.modules.manage.entity.StudentInfoEntity;
import cn.internship.modules.manage.entity.dto.CompanySearch;
import cn.internship.modules.manage.entity.dto.StudentSearch;
import cn.internship.modules.manage.service.StudentInfoService;
import cn.internship.modules.sys.controller.AbstractController;
import cn.internship.modules.sys.entity.SysUserRoleEntity;
import cn.internship.modules.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.internship.modules.manage.entity.StuInternCompanyEntity;
import cn.internship.modules.manage.service.StuInternCompanyService;
import cn.internship.common.utils.PageUtils;
import cn.internship.common.utils.R;

import javax.annotation.Resource;


/**
 * 
 *
 * @author haha
 * @email haha@gmail.com
 * @date 2023-02-20 18:17:09
 */
@RestController
@RequestMapping("manage/stuinterncompany")
public class StuInternCompanyController extends AbstractController {
    @Autowired
    private StuInternCompanyService stuInternCompanyService;

    @Resource
    protected StudentInfoService studentInfoService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    @RequiresPermissions("manage:stuinterncompany:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = stuInternCompanyService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    @RequestMapping("/list")
    @RequiresPermissions("manage:stuinterncompany:list")
    public R list(CompanySearch search){
        //获取用户ID
        Long userId = getUserId();
        //通过用户ID查询角色
        LambdaQueryWrapper<SysUserRoleEntity> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(SysUserRoleEntity::getUserId,userId);
        SysUserRoleEntity one = sysUserRoleService.getOne(queryWrap);
        if (one == null){
            return R.error("该用户不属于任何角色");
        }
        if (one.getRoleId() == 2){
            search.setTeaUserId(userId);
        }
        if (one.getRoleId() == 3){
            search.setStuUserId(userId);
        }
        PageUtils page = stuInternCompanyService.listByPage(search);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("manage:stuinterncompany:info")
    public R info(@PathVariable("id") Long id){
		StuInternCompanyEntity stuInternCompany = stuInternCompanyService.getById(id);

        return R.ok().put("stuInternCompany", stuInternCompany);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("manage:stuinterncompany:save")
    public R save(@RequestBody StuInternCompanyEntity stuInternCompany){
        //获取用户名
        LambdaQueryWrapper<StudentInfoEntity> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(StudentInfoEntity::getUserId,getUserId());
        StudentInfoEntity stuInfoEntity = studentInfoService.getOne(queryWrap);
        if (stuInfoEntity == null){
            return R.error("用户不存在");
        }
        stuInternCompany.setStuId(stuInfoEntity.getId());
        stuInternCompanyService.save(stuInternCompany);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("manage:stuinterncompany:update")
    public R update(@RequestBody StuInternCompanyEntity stuInternCompany){
		stuInternCompanyService.updateById(stuInternCompany);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("manage:stuinterncompany:delete")
    public R delete(@RequestBody Long[] ids){
		stuInternCompanyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
